// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AudioManage, {soundList} from "../../framework/AudioManage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class endLessScene extends cc.Component {

    @property(cc.Node)
    node1 : cc.Node = null;

    @property(cc.Node)
    node2 : cc.Node = null;

    @property(cc.Node)
    node3 : cc.Node = null;

    @property(cc.Node)
    nodeList : cc.Node[] = [];

    onLoad () {
        this.colorLineAction(this.node1);
        this.colorLineAction(this.node2);
        this.fontAction();
        this.schedule(this.starAction,2);
        AudioManage.playSFX(soundList.colour);
    }

    protected onDestroy(): void {
        this.unschedule(this.starAction);
    }


    colorLineAction(node){
        cc.tween(node).by(2,{
            scale : 0.1
        }).call(()=>{
            cc.tween(node).by(2,{
                scale : -0.1
            }).call(()=>{
                this.colorLineAction(node);
            }).start()
        }).start()
    }


    fontAction(){
        cc.tween(this.node3).to(0.2,{
            scale : 1.5
        }).call(()=>{
            cc.tween(this.node3).delay(2).call(()=>{
                cc.tween(this.node3).to(0.2,{
                    scale : 0
                }).start();
            }).start();
        }).start();
    }


    starAction(){
        for(let i in this.nodeList){
            let random = Math.random() > 0.5 ? 1 : -1;
            cc.tween(this.nodeList[i]).by(0.2,{
                angle : random * 45
            }).start();
        }
        let length = this.nodeList.length;
        let index = Math.random() * length | 0;
        cc.tween(this.nodeList[index]).by(1,{
            scale : 0.5
        }).call(()=>{
            cc.tween(this.nodeList[index]).by(1,{
                scale : -0.5
            }).start();
        }).start();
    }
}
